package com.github.alfred.common; /**
 * 版权所有：厦门市巨龙信息科技有限公司
 * Copyright 2023 Xiamen Dragoninfo Eng. Co. Ltd.
 * All right reserved.
 */


import com.github.alfred.enums.IErrorCodeEnum;

import java.io.Serializable;

/**
 * 类说明
 *
 * @author chengh
 * @date 2023/4/10 13:30
 */


public class Result<T> implements Serializable {

    private static final long serialVersionUID = 6062002582116139231L;

    public static String SUCCESS_CODE = "200";
    public static String FAIL_CODE = "500";

    private String code;
    private String errorCode;

    private String msg;

    private T data;


    public static <T> Result<T> success() {
        return fill(null, SUCCESS_CODE, null, "success");
    }

    public static <T> Result<T> fail() {
        return error("UNKNOWN_ERROR");
    }

    public static <T> Result<T> success(T data) {
        return fill(data, SUCCESS_CODE, null, "success");
    }

    public static <T> Result<T> success(String code, String msg) {
        return fill(null, FAIL_CODE, code, msg);
    }

    public static <T> Result<T> error(String msg) {
        return fill(null, FAIL_CODE, FAIL_CODE, msg);
    }

    public static <T> Result<T> error(String msg, T data) {
        return fill(data, FAIL_CODE, FAIL_CODE, msg);
    }

    public static <T> Result<T> fail(String code, String msg) {
        return fill(null, FAIL_CODE, code, msg);
    }

    public static <T> Result<T> fail(IErrorCodeEnum errorCodeEnum) {
        return fill(null, FAIL_CODE, errorCodeEnum.getErrorCode(), errorCodeEnum.getErrorMsg());
    }


    public static <T> Result<T> fill(T data, String code, String errorCode, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setErrorCode(errorCode);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
